//
//  MHOptionsViewController.m
//  MHO
//
//  Created by papo on 2/24/14.
//  Copyright (c) 2014 maihoang. All rights reserved.
//

#import "MHOptionsViewController.h"
#import "MHAppDelegate.h"
#import "MHSettingViewController.h"
#import "Lib.h"
#import "MHDownloadViewController.h"
#import "MHFAQViewController.h"
#import "MHFeedbackViewController.h"

@interface MHOptionsViewController ()
{
    NSArray*    titleArray;
}
@end

@implementation MHOptionsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
//    self.navibarView = [MHNavibarView createWithTitle:@"Tuy chon" leftButton:kFileBtnHuman rightButton:nil searchButton:NO];
//    [self.navibarView setActionSelectorDelegate:self left:@selector(backButtonPressed:) right:nil search:nil];
//    [self.navibarView setRootViewController:self];
//    [self.view addSubview:self.navibarView];
    CGRect frame = self.tableView.frame;
    frame.origin.y = self.navibarView.frame.size.height;
    frame.size.height -= self.navibarView.frame.size.height;
    self.tableView.frame = frame;
//    self.t
    
    titleArray = @[@"Thiết lập cơ bản", @"FAQ", @"Góp ý", @"Xóa bỏ nhớ"];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return titleArray.count;
}

//- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
//    return self.navibarView;
//}
//
//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
//    return self.navibarView.frame.size.height;
//}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        if (indexPath.row < 3) {
            [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
        }
        else {
//            UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
//            [button setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
//            [button addTarget:self action:@selector(deleteButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
//            [button setFrame:CGRectMake(270, 5, 50, 34)];
//            button.tag = 123;
//            [cell.contentView addSubview:button];
            
            //
            UILabel* size = [[UILabel alloc] initWithFrame:CGRectMake(200, 5, 80, 34)];
            [size setBackgroundColor:[UIColor clearColor]];
            [size setTextAlignment:NSTextAlignmentRight];
            size.tag = 123;
            [cell.contentView addSubview:size];
            
        }
    }
    cell.textLabel.text = titleArray[indexPath.row];
    
    // Configure the cell...
    [cell.contentView sendSubviewToBack:cell.textLabel];
    UILabel* lb = (UILabel*)[cell.contentView viewWithTag:123];
    if (lb) {
        [lb setText:[NSString stringWithFormat:@"%llu MB", [Lib sizeOfFolder:[[TCBlobDownloadManager sharedInstance] defaultDownloadPath]] / 1024 / 1024]];
    }
    
    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark - Table view delegate

// In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here, for example:
    // Create the next view controller.
    if (indexPath.row == 0) {
        MHSettingViewController *detailViewController = [[MHSettingViewController alloc] initWithNibName:@"MHSettingViewController" bundle:nil];

        // Pass the selected object to the new view controller.

        // Push the view controller.
        [self.navigationController pushViewController:detailViewController animated:YES];
    }
    else if (indexPath.row == 1) {
        MHFAQViewController* vc = [[MHFAQViewController alloc] initWithNibName:@"MHFAQViewController" bundle:nil];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if (indexPath.row == 2) {
        MHFeedbackViewController* vc = [[MHFeedbackViewController alloc] initWithNibName:@"MHFeedbackViewController" bundle:nil];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if (indexPath.row == 3) {
//        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Cảnh báo!" message:@"Bạn có chắc muốn xoá hết bộ nhớ không?" delegate:self cancelButtonTitle:@"Không" otherButtonTitles:@"Có", nil];
//        alert.tag = 123;
//        [alert show];
        [UIAlertView showWithTitle:@"Cảnh báo!" message:@"Bạn có chắc muốn xoá hết bộ nhớ không?" cancelButtonTitle:@"Không" otherButtonTitles:@[@"Có"] tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
            if (buttonIndex == 1) {
                NSString* folder = [[TCBlobDownloadManager sharedInstance] defaultDownloadPath];
                NSArray* files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:folder error:nil];
                for (NSString* file in files) {
                    NSString* p = [folder stringByAppendingPathComponent:file];
                    [[NSFileManager defaultManager] removeItemAtPath:p error:nil];
                }
                NSString* docFolder = [Lib getDocumentPath];
                NSString* dictFile = [docFolder stringByAppendingPathComponent:@"download.plist"];
                [[NSFileManager defaultManager] removeItemAtPath:dictFile error:nil];
                
                [self.tableView reloadData];
            }
        }];
    }
}
 
 

- (void)deleteButtonPressed: (id)sender {
    
}

- (void)backButtonPressed:(id)sender {
    [[MHAppDelegate sharedApp ].viewController showLeftPanelAnimated:YES];
}

- (void)leftButtonPressed:(id)button {
    [self backButtonPressed:button];
}



#pragma mark - UIAlertView Delegate 
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView.tag == 123 && buttonIndex == 1) {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Thông báo" message:@"Đã xoá bộ nhớ thành công!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    }
}
@end
